<?php
class weebUIVolumeManager
{
    /**
    * Volumes mapping
    * @todo replace with an analysis of USBDLM.ini
    **/
    static $mapping = array( 'HD EXT 1'  => array( 'TV Shows' => 'k:\TV Shows', 'Movies' => 'k:\Movies' ),
                             'HD EXT 2'  => array( 'TV Shows' => 'l:\TV Shows', 'Movies' => 'l:\Movies' ),
                             'HD EXT 3'  => array( 'TV Shows' => 'm:\TV Shows', 'Movies' => 'm:\Movies' ),
                             'HD EXT 4'  => array( 'TV Shows' => 'n:\TV Shows', 'Movies' => 'n:\Movies' ),
                             'ARTHAS'    => array( 'TV Shows' => 'o:\TV Shows', 'Movies' => 'o:\Movies' ),
                             'KARMACOMA' => array( 'TV Shows' => 'z:', 'Movies' => 'x:' ) );

    /**
    * Loads the volume manager for a given library / volume
    * @param int $libraryID
    * @param string $volumeName
    * @return weebUIVolumeManager
    * @throws weebUIVolumeManagerUnknownVolumeExtension if the volume and / or
    *         library are not found in the mapping
    **/
    public static function loadVolume( $libraryID, $volumeName )
    {
        $libraryName = weebUIMovieListFunctions::getLibrary( $libraryID, 'name' );
        if ( !isset( self::$mapping[ $volumeName ] ) )
            throw new weebUIVolumeManagerUnknownVolumeExtension( array( $libraryName, $volumeName ) );
        elseif ( !isset( self::$mapping[ $volumeName ][ $libraryName ] ) )
        {
            throw new weebUIVolumeManagerUnknownVolumeExtension( array( $libraryName, $volumeName ) );
        }
        else
        {
            $manager = new weebUIVolumeManager();
            $manager->name = $volumeName;
            $manager->path = self::$mapping[ $volumeName ][ $libraryName ];
            return $manager;
        }
    }

    public static function volumesList()
    {
        /**
        * @todo Reimplement this SQL query to get items count per volume ?
         global $db, $tag_VolumeName;

         $list = array();
         $query = "SELECT COUNT(*) AS ItemsCount, $tag_VolumeName AS VolumeName FROM items GROUP BY $tag_VolumeName ORDER BY VolumeName";

         $res_volumeNames = sqlite_query( $db, $query, SQLITE_ASSOC );
         while ( $row = sqlite_fetch_array( $res_volumeNames ) )
         {
         $list[] = $row;
         }
         return $list;
        */

        $list = array_keys( self::$mapping );
        sort( $list );
        return $list;
    }

    /**
    * Volume name
    **/
    var $name;
    var $path;
}

class weebUIVolumeManagerUnknownVolumeExtension extends Exception
{
    public function __construct( $messageData )
    {
        $this->message = "Volume not found (library: $messageData[0], volume: $messageData[1]";
    }
}
?>